
package com.jf.cloud.flow.controller.app;

import cn.hutool.core.util.StrUtil;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.util.IpHelper;
import com.jf.cloud.flow.constant.FlowLogPageEnum;
import com.jf.cloud.flow.constant.FlowVisitEnum;
import com.jf.cloud.flow.dto.FlowLogDTO;
import com.jf.cloud.flow.service.FlowService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * 流量分析—页面分析
 *
 * @author zz
 * @date 2021-05-21 15:25:19
 */
@RestController("appPageAnalyseController")
@RequestMapping("/ma/flow")
@Tag(name = "app流量分析—页面分析")
public class FlowLogController {

    private static final Logger log = LoggerFactory.getLogger(FlowLogController.class);

    @Autowired
    private FlowService flowService;

    private static final Pattern SPU_ID_PATTERN = Pattern.compile("[0-9]+");
    private static final Integer USER_ID_LENGTH = 12;

    @PostMapping
    @Operation(summary = "保存用户操作信息", description = "保存用户操作信息")
    public ServerResponseEntity<Void> page(@RequestBody FlowLogDTO flowLogDTO, HttpServletRequest request) {
        // 数据为空，打印日志
        if (checkDate(flowLogDTO)) {
            return ServerResponseEntity.success();
        }
        flowLogDTO.setIp(IpHelper.getIpAddr());
        flowService.log(flowLogDTO);
        return ServerResponseEntity.success();
    }

    /**
     * 数据是否错误
     * @param flowLogDTO
     * @return
     */
    private Boolean checkDate(FlowLogDTO flowLogDTO) {
        if (Objects.isNull(flowLogDTO.getPageId()) || StrUtil.isBlank(flowLogDTO.getUuid()) ||
                Objects.isNull(flowLogDTO.getVisitType()) || Objects.isNull(flowLogDTO.getStep()) || Objects.isNull(flowLogDTO.getSystemType())) {
            log.error("用户流量记录-基本数据不全：{}", flowLogDTO);
            return Boolean.TRUE;
        }
        // 过滤掉不符合规则的userid&&uuid的数据
        boolean shouldFilterUserId = StrUtil.isNotBlank(flowLogDTO.getUserId()) && (StrUtil.hasBlank(flowLogDTO.getUserId()) || !Objects.equals(flowLogDTO.getUserId().length(), USER_ID_LENGTH));
        if (shouldFilterUserId) {
            // 用户id是12位的并且不包括空格
            log.error("用户流量记录-用户id格式错误：" + flowLogDTO);
            return Boolean.TRUE;
        }
        boolean shouldFilterUuid = StrUtil.isNotBlank(flowLogDTO.getUuid()) && (StrUtil.hasBlank(flowLogDTO.getUuid()) || !flowLogDTO.getUuid().matches(Constant.UUID_PATTERN));
        if (shouldFilterUuid) {
            // uuid正则校验
            log.error("用户流量记录-uuid格式错误：" + flowLogDTO);
            return Boolean.TRUE;
        }
        if (Objects.equals(flowLogDTO.getVisitType(), FlowVisitEnum.SHOP_CAT.value())) {
            if (Objects.isNull(flowLogDTO.getBizData())) {
                log.error("用户流量记录-购物车:商品id为空{}", flowLogDTO);
                return Boolean.TRUE;
            }
            if (Objects.isNull(flowLogDTO.getNums())) {
                log.error("用户流量记录-加购:加购数量为空{}", flowLogDTO);
                return Boolean.TRUE;
            }
        }
        if (Objects.equals(flowLogDTO.getPageId(), FlowLogPageEnum.PROD_INFO.value())) {
            if (Objects.isNull(flowLogDTO.getBizData())) {
                log.error("用户流量记录-商品详情:商品id数据错误{}", flowLogDTO);
                return Boolean.TRUE;
            }
            if (!SPU_ID_PATTERN.matcher(flowLogDTO.getBizData()).matches()) {
                log.error("用户流量记录-商品详情:商品id数据错误" + flowLogDTO);
                return Boolean.TRUE;
            }
        }
        if (Objects.equals(flowLogDTO.getPageId(), FlowLogPageEnum.PAY.value()) && Objects.isNull(flowLogDTO.getBizData())) {
            log.error("用户流量记录-支付:支付单号为空{}", flowLogDTO);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }


}
